package com.rongshu.view.web;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.rongshu.view.base.BaseActivity;

import org.json.JSONException;
import org.json.JSONObject;


public class JavaScriptObject {

    Context mContext;


    public JavaScriptObject(Context mContext) {
        this.mContext = mContext;
    }

    @JavascriptInterface
    public void doBusiness(String json) {
        Intent intent = new Intent();
        if (!TextUtils.isEmpty(json)) {
            if (getJSONField(json, "?") != null) {
                String couponUse = getJSONField(json, "?").toString();
                intent.putExtra("?", couponUse);
            }
            ((BaseActivity) mContext).setResult(Activity.RESULT_OK, intent);
            ((BaseActivity) mContext).finish();
        }


    }

    /**
     * 获取JSON字段
     *
     * @param params    json字符串
     * @param fieldName 字段名称
     */
    private Object getJSONField(String params, String fieldName) {

        Object field = null;
        try {
            JSONObject object = new JSONObject(params);
            field = object.opt(fieldName);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return field;
    }
}
